<?php
/*
 * description：
 * author：wh
 * email：
 * createTime：{2023/12/31} {21:56} 
 */

namespace wanghua\general_utility_tools_php;


use app\common\model\TabConf;
use think\Db;
use wanghua\general_utility_tools_php\tool\Tools;

/**
 * 系统杂项配置
 *
 * 读取fa_sundry_config表中的配置
 *
 * Class SundryConfig
 * @package wanghua\general_utility_tools_php
 */
class SundryConfig
{
    private static $tablename = 'fa_zc_sundry_config';

    /**
     * desc：获取配置的值，设置配置的值
     *
     * 默认缓存值
     *
     * author：wh
     * @param string $key
     * @param string $val
     * @return float|mixed|string
     */
    static function val(string $key,string $val=''){
        $tabname = self::$tablename;
        if($val){
            $obj = Db::table($tabname);
            $obj->where('key',$key);
            $obj->data(['val'=>$val]);
            return $obj->update();
        }
        $obj = Db::table($tabname);
        return $obj->where(['key'=>$key])
            ->cache($tabname.'_'.$key,0,$tabname)
            ->value('val');
    }
    /**
     * desc：根据分组获取配置
     */
    static function valGroup(string $key){
        $tabname = self::$tablename;

        $obj = Db::table($tabname);
        $arr = $obj->where(['group'=>$key])
            ->cache($tabname.'_group_'.$key,0,$tabname)
            ->select();
        return Tools::key_val_arr($arr,'key','val');
    }


    /**
     * desc：清除指定key的缓存
     * author：wh
     * @param string $key
     * @return bool
     */
    static function clearCache(string $key){
        $tabname = self::$tablename;
        return cache($tabname.'_'.$key, null);
    }

    /**
     * desc：清除指定分组的缓存
     * author：wh
     * @param string $group
     * @return bool
     */
    static function clearGroupCache(string $group){
        $tabname = self::$tablename;
        return cache($tabname.'_group_'.$group, null);
    }
}